package com.bawei.demo_shoppingcar.activity;

import android.app.AlertDialog;
import android.app.Instrumentation;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bawei.demo_shoppingcar.R;
import com.bawei.demo_shoppingcar.application.ContentData;
import com.bawei.demo_shoppingcar.bean.CommdityBean;
import com.bawei.demo_shoppingcar.bean.ShoppingDBbean;
import com.bawei.demo_shoppingcar.db.ShoppingDBUtils;
import com.bawei.demo_shoppingcar.view.AmountView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CommodityActivity extends AppCompatActivity implements View.OnClickListener {

    private CommdityBean data;
    private ImageView image;
    private TextView name;
    private TextView price;
    private String goods_img;
    private String goods_name;
    private double shop_price;
    private Button bt_buy;
    private Button bt_add;
    private AmountView viewById;
    private AlertDialog.Builder alertDialog;
    private ImageView image1;
    private TextView price1;
    private View inflate;
    private ShoppingDBUtils instanceDBUtils;
    private AlertDialog.Builder buyDialog;

    private int amountCommodit = 1;
    private ImageView commodity_back;

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            int index = msg.what;
            if (index == 1) {

            } else if (index == 2) {
                buyDialog = new AlertDialog.Builder(getApplication());
                View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.buy_dialog, null);
                buyDialog.setView(view);
                buyDialog.show();
                finish();
            }

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity);
        intView();
        intData();
        setData();

    }

    private void setData() {

        name.setText(goods_name);
        price.setText(shop_price + "¥");
        ImageLoader.getInstance().displayImage(goods_img, image, ContentData.options);
    }

    private void intData() {
        instanceDBUtils = ShoppingDBUtils.getInstance(this);
        Intent intent = getIntent();
        data = (CommdityBean) intent.getSerializableExtra("data");
        goods_img = data.getImage();
        goods_name = data.getName();
        shop_price = data.getPrice();
    }

    private void intView() {
        commodity_back = (ImageView) findViewById(R.id.commodity_back);
        image = (ImageView) findViewById(R.id.commodity_image);
        name = (TextView) findViewById(R.id.commodity_name);
        price = (TextView) findViewById(R.id.commodity_price);
        bt_add = (Button) findViewById(R.id.goods_bt_add);
        bt_buy = (Button) findViewById(R.id.goods_bt_buy);
//        viewById = (AmountView) findViewById(R.id.amount_view);
        bt_add.setOnClickListener(this);
        bt_buy.setOnClickListener(this);
        commodity_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.goods_bt_add:
                displayDialog(true);
                break;
            case R.id.goods_bt_buy:
                displayDialog(false);
                break;
            case R.id.commodity_back:

                new Thread() {
                    @Override
                    public void run() {
                        Instrumentation inst = new Instrumentation();
                        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                    }
                }.start();

                break;
        }
    }

    //弹框
    private void displayDialog(final boolean isGoos) {

        boolean login_qq = ContentData.isLogin_QQ();
        if (login_qq) {

            alertDialog = new AlertDialog.Builder(this);
            initDisplayView();

            ImageLoader.getInstance().displayImage(goods_img, image1, ContentData.options);
            price1.setText(shop_price + "¥");
            alertDialog.setView(inflate);
            alertDialog.setNegativeButton("确定", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    if (isGoos) {//添加到购物车
                        ShoppingDBbean bean = new ShoppingDBbean(0, goods_name, shop_price, amountCommodit, goods_img);
                        instanceDBUtils.insertData(bean);

                    } else {//立即购买

                    }
                    finish();
                }
            });

            alertDialog.show();
            this.viewById.setGoods_storage(5);
            this.viewById.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
                @Override
                public void onAmountChange(View view, int amount) {
                    amountCommodit = amount;
                }
            });
        } else {
            startActivity(new Intent(getApplicationContext(), UserRegisterActivity.class));
        }
    }

    private void initDisplayView() {

        inflate = LayoutInflater.from(this).inflate(R.layout.add_shoppding_dialog, null);
        viewById = (AmountView) inflate.findViewById(R.id.amount_view);
        image1 = (ImageView) inflate.findViewById(R.id.dialog_image);
        price1 = (TextView) inflate.findViewById(R.id.dialog_price);

    }


}
